package com.kermel.andromeda.scheduler;

import android.app.Activity;
import android.app.AlarmManager;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;

import com.kermel.andromeda.R;
import com.kermel.andromeda.data.Alarm;

public class AlarmScheduler {
    private Context context;
    private AlarmManager alarmManager;
    private NotificationManager notificationManager;
    
    public AlarmScheduler(Context context) {
        this.context = context;
        
        retrieveServices();
    }
    
    private void retrieveServices() {
        retrieveAlarmManagerService();
        retrieveNotificationManagerService();
    }
    
    private void retrieveAlarmManagerService() {
        if (alarmManager == null) {
            Object service = context.getSystemService(Context.ALARM_SERVICE);
            if (service != null && service instanceof AlarmManager) {
                alarmManager = (AlarmManager)service;
            }
        }
    }
    
    private void retrieveNotificationManagerService() {
        if (notificationManager == null) {
            Object service = context.getSystemService(Context.NOTIFICATION_SERVICE);
            if (service != null && service instanceof NotificationManager) {
                notificationManager = (NotificationManager)service;
            }
        }
    }
    
    public int scheduleAlarm(Alarm alarm, Class<? extends Activity> activityClass) {
        int alarmID = Alarm.UNKNOWN_ALARM_NOTIFICATION_ID;
        
        if (alarm != null) {
            PendingIntent operation = createAndScheduleAlarmOperationWithAlarmManager(alarm, activityClass);
            alarmID = alarm.getAlarmNotificationID();
            displayStatusNotification(operation, alarmID);
        }
        
        return alarmID;
    }
    
    private PendingIntent createAndScheduleAlarmOperationWithAlarmManager(Alarm alarm, Class<? extends Activity> activityClass) {
        long alarmTimeInMilliseconds = alarm.triggerTime;
        Intent intent = createAlarmActivityIntent(alarm, activityClass);
        PendingIntent operation = PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_ONE_SHOT);
        alarmManager.set(AlarmManager.RTC_WAKEUP, alarmTimeInMilliseconds, operation);
        return operation;
    }
    
    private Intent createAlarmActivityIntent(Alarm alarm, Class<? extends Activity> activityClass) {
        Intent intent = new Intent(context, activityClass);
        intent.putExtra(Alarm.ALARM_PARCEL_NAME, alarm);
        return intent;
    }
    
    private void displayStatusNotification(PendingIntent operation, int alarmNotificationID) {
        Notification notification = new Notification(R.drawable.stat_notify_alarm, "", System.currentTimeMillis());
        notification.flags |= Notification.FLAG_SHOW_LIGHTS | Notification.FLAG_ONGOING_EVENT;
        notification.defaults |= Notification.DEFAULT_LIGHTS;
        notification.setLatestEventInfo(context, "", "", operation);
        notificationManager.notify(alarmNotificationID, notification);
    }
    
    public void removeStatusNotification(int alarmNotificationID) {
        notificationManager.cancel(alarmNotificationID);
    }
    
}
